#include <iostream>
#include <climits>
#include <algorithm>

const int MAXN = 200010;
int father[MAXN];
int path[MAXN];
int stack[MAXN];
int n;
int result = INT_MAX;

void Build() {
	for (int i = 0; i <= n; i++) {
		father[i] = i;
	}
}

int Find(int i) {
	int stackSize = 0;

	while (i != father[i]) {
		stack[stackSize++] = i;
		i = father[i];
	}

	while (stackSize > 0) {
		const int index = --stackSize;
		path[stack[index]] += path[father[stack[index]]];
		father[stack[index]] = i;
	}

	return i;
}



void Union(const int x, const int y) {
	const int fx = Find(x);
	const int fy = Find(y);

	if (fx != fy) {
		father[fx] = fy;
		path[x] = path[y] + 1;
	}
	else {
		result = std::min(result, path[x] + path[y] + 1);
	}


}

/**
 * 信息传递
 */
int main() {
	std::cin >> n;

	Build();

	for (int x = 1; x <= n; x++) {
		int y;
		std::cin >> y;

		Union(x, y);
	}


	std::cout << result;

	return 0;
}